package com.itheima.collection.List.arrayList;

import java.util.*;

public class ArrayListTest {

 public static void main(String[] args) {
  // 创建一个 Student 数组
  Student[] studentsArray = new Student[]{
          new Student(1, 18, "zhangsan"),
          new Student(2, 21, "lisi"),
          new Student(3, 18, "wangwu")
  };

  // 将数组转换为 List
  List<Student> studentList = Arrays.asList(studentsArray);

  // 打印原始数组和列表
  System.out.println("原始数组:");
  System.out.println(Arrays.toString(studentsArray));
  System.out.println("原始列表:");
  System.out.println(studentList);

  // 修改数组中的元素
  studentsArray[1] = new Student(4, 22, "newStudent");
  System.out.println("\n修改数组后的结果:");
  System.out.println("数组:");
  System.out.println(Arrays.toString(studentsArray));
  System.out.println("列表:");
  System.out.println(studentList);  // 列表会显示数组的更改

  // 尝试修改列表
  try {
   studentList.set(2, new Student(5, 25, "anotherStudent"));  // 修改列表中的元素是允许的
   System.out.println("\n修改列表后的结果:");
   System.out.println("数组:");
   System.out.println(Arrays.toString(studentsArray));  // 数组的内容也会更新
   System.out.println("列表:");
   System.out.println(studentList);
  } catch (UnsupportedOperationException e) {
   System.out.println("修改列表失败: " + e.getMessage());
  }

  // 将列表转换为新数组
  System.out.println("将列表转化为新数组:");
  Student[] newArray = studentList.toArray(new Student[0]);

  // 打印新数组
  System.out.println("\n转换后的新数组:");
  System.out.println(Arrays.toString(newArray));

  // 修改新数组中的元素
  newArray[0] = new Student(6, 30, "modifiedInArray");
  System.out.println("\n修改新数组后的结果:");
  System.out.println("新数组:");
  System.out.println(Arrays.toString(newArray));
  System.out.println("列表:");
  System.out.println(studentList);  // 列表不会受到新数组的修改影响
 }
}
